package cheme.ProcessModel.ModelActions;

import java.beans.PropertyChangeEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * Represent a collection of actions associated with an event that can execute as a group.
 * 
 *  Note that as of the first version of Cheme, the actions do not take the form 
 *  of Commands are not routed through the command stack architecture... this means that 
 *  these actions are not undoable :(.
 *  
 *  In the future such actions would hook the comamnd stack, and simply chain additional 
 *  commands to a triggering command.
 */
public interface IModelActionRunner
{
	void add(IModelAction rule);
	void run(PropertyChangeEvent evt);
	
	public class ModelActionRunner implements IModelActionRunner
	{
		private List<IModelAction> modelActions = null;
		
		public void add(IModelAction rule)
		{
			if(null == modelActions)
			{
				modelActions = new ArrayList<IModelAction>();
			}
			
			modelActions.add(rule);
		}

		public void run(PropertyChangeEvent evt)
		{			
			if(null != modelActions)
			{
				Iterator<IModelAction> itr = modelActions.iterator();
				IModelAction current = null;
				
				/*
				 * break out on the first rule failure
				 */
				while(itr.hasNext())
				{
					current = itr.next();
					current.run(evt);
				}
			}
		}	
	}
}
